<template>
  <div class="w-card content-box">
    <div class="title-box">
      <span class="title">财务报表</span>
    </div>
    <div class="search-box">
      <div>
        <span>月报时间：</span>
        <el-date-picker v-model="searchForm.date" type="monthrange" range-separator="至" start-placeholder="开始月份"
          end-placeholder="结束月份" />
      </div>
      <el-button type="primary" @click="searchHandle">搜索</el-button>
      <el-button style="margin-left: 0;" @click="resetHandle">重置</el-button>
    </div>
    <div class="table-box">
      <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column prop="month" label="报表月份" align="center">
          <template v-slot="scope">
            {{ scope.row.month || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="生成时间" align="center">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="operations" label="操作" align="center" width="160">
          <template v-slot="scope">
            <el-link style="font-size:14px;" type="primary" @click="downloadHandle(scope.row.month)">下载</el-link>
            <el-link style="font-size:14px;margin-left: 10px;" type="primary"
              @click="reloadHandle(scope.row.month)">重新生成</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-footer">
        <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
          :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { warningDeal, getDateTime, BlobDownLoad, successDeal, getWindowWidthAndHeight } from '@/utils/utils'
import { onMounted, reactive, toRefs } from 'vue'
import api from '../api'
export default {
  setup() {
    const state = reactive({
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      searchForm: {},
      tableData: [],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...getMonth(state.searchForm.date),
      }
      api.staticsCaiwuList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getMonth(list) {
      const myObj = {}
      if (!list || list.length == 0) return
      const oy = list[0].getFullYear()
      const om = list[0].getMonth() + 1
      const ty = list[1].getFullYear()
      const tm = list[1].getMonth() + 1
      myObj.monthMin = '' + oy + ((om) > 9 ? (om) : '0' + (om))
      myObj.monthMax = '' + ty + ((tm) > 9 ? (tm) : '0' + (tm))
      return myObj
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function downloadHandle(fileId) {
      const obj = {
        fileId,
        responseType: "blob",
      }
      api.staticsCaiwuDown(obj).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    function reloadHandle(fileId) {
      const obj = {
        fileId
      }
      api.staticsCaiwuRepair(obj).then(res => {
        if (res.code == '200') {
          getTableData()
        }
        warningDeal(res.msg)
      }).catch(err => {
        console.log(err)
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 280
      getTableData()
    })

    return {
      ...toRefs(state),
      searchHandle,
      resetHandle,
      getDateTime,
      downloadHandle,
      reloadHandle,
      handleSizeChange,
      handleCurrentChange,
    }
  }
}
</script>

<style lang="scss" scoped>
.content-box {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  .title-box {
    font-size: 16px;
    font-weight: bold;
    box-sizing: border-box;
    padding: 20px;
    border-bottom: 1px solid #d0d3d6;
  }
  .search-box {
    display: flex;
    align-items: center;
    gap: 10px;
    box-sizing: border-box;
    padding: 20px;
    font-size: 14px;
  }
  .table-box {
    box-sizing: border-box;
    padding: 0 20px;
    .table-footer {
      margin-top: 10px;
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>